Fundamentos y métodos
2025-02-19
Los gradientes se aproximan con operadores de convolución:
Roberts \[ R_x = \begin{bmatrix} 1 & 0 \\ 0 & -1 \end{bmatrix}, \quad R_y = \begin{bmatrix} 0 & 1 \\ -1 & 0 \end{bmatrix} \]
Prewitt \[ P_x = \begin{bmatrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{bmatrix}, \quad P_y = \begin{bmatrix} -1 & -1 & -1 \\ 0 & 0 & 0 \\ 1 & 1 & 1 \end{bmatrix} \]
Sobel (más usado) \[ S_x = \begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}, \quad S_y = \begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix} \]
import cv2
import matplotlib.pyplot as plt
img = cv2.imread("imagen.png", 0)
edges_roberts = cv2.filter2D(img, -1, np.array([[1,0],[0,-1]]))
edges_prewittx = cv2.filter2D(img, -1, np.array([[-1,0,1],[-1,0,1],[-1,0,1]]))
edges_sobel = cv2.Sobel(img, cv2.CV_64F, 1, 1, ksize=3)
edges_canny = cv2.Canny(img, 100, 200)
plt.figure(figsize=(10,8))
plt.subplot(2,2,1); plt.imshow(edges_roberts, cmap="gray"); plt.title("Roberts")
plt.subplot(2,2,2); plt.imshow(edges_prewittx, cmap="gray"); plt.title("Prewitt X")
plt.subplot(2,2,3); plt.imshow(edges_sobel, cmap="gray"); plt.title("Sobel")
plt.subplot(2,2,4); plt.imshow(edges_canny, cmap="gray"); plt.title("Canny")
plt.show()CIMAT – INFOTEC